﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;

namespace COER.Manual
{
    public partial class UC_ManualVisionLight : UserControl
    {
        public UC_ManualVisionLight()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                }

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        public void Load()
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible == false) return;

            FreshStatus();
        }

        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                #region 光源控制
                case "Exc_CodeRead":
                    {
                        ProcedureHelper_Exc.ExcST_CodeReadLightOn = ProcedureHelper_Exc.ExcST_CodeReadLightOn ? false : true;
                    }
                    break;
                case "Exc_Ball":
                    {
                        ProcedureHelper_Exc.ExcST_BallLightOn = ProcedureHelper_Exc.ExcST_BallLightOn?false:true;
                    }
                    break;
                case "Exc_Coaxial":
                    {
                        ProcedureHelper_Exc.ExcST_CoaxialLightOn = ProcedureHelper_Exc.ExcST_CoaxialLightOn ? false : true;
                    }
                    break;
                case "SortST_PreAlign":
                    {
                        ProcedureHelper_Exc.SortST_PreAlignLightOn = ProcedureHelper_Exc.SortST_PreAlignLightOn ? false : true;
                    }
                    break;
                case "SortST_FinalAlign":
                    {
                        ProcedureHelper_Exc.SortST_FinalAlignLightOn = ProcedureHelper_Exc.SortST_FinalAlignLightOn ? false : true;
                    }
                    break;
                case "LaserST_UpperCoaxial":
                    {
                        ProcedureHelper_Exc.LaserST_UpperCoaxialLightOn = ProcedureHelper_Exc.LaserST_UpperCoaxialLightOn ? false : true;
                    }
                    break;
                case "LaserST_UpperBall":
                    {
                        ProcedureHelper_Exc.LaserST_UpperBallLightOn = ProcedureHelper_Exc.LaserST_UpperBallLightOn ? false : true;
                    }
                    break;
                case "LaserST_SideCoaxial":
                    {
                        ProcedureHelper_Exc.LaserST_SideCoaxialLightOn = ProcedureHelper_Exc.LaserST_SideCoaxialLightOn ? false : true;
                    }
                    break;
                case "LaserST_SideBack":
                    {
                        ProcedureHelper_Exc.LaserST_SideBackLightOn = ProcedureHelper_Exc.LaserST_SideBackLightOn ? false : true;
                    }
                    break;
                case "LaserST_InspCoaxial":
                    {
                        ProcedureHelper_Exc.LaserST_InspCoaxialLightOn = ProcedureHelper_Exc.LaserST_InspCoaxialLightOn ? false : true;
                    }
                    break;
                case "LaserST_InspBall":
                    {
                        ProcedureHelper_Exc.LaserST_InspBallLightOn = ProcedureHelper_Exc.LaserST_InspBallLightOn ? false : true;
                    }
                    break;
                case "ULDTR1_Align":
                    {
                        ProcedureHelper_Exc.ULDTR1_AlignLightOn = ProcedureHelper_Exc.ULDTR1_AlignLightOn ? false : true;
                    }
                    break;
                case "ULDTR2_Align":
                    {
                        ProcedureHelper_Exc.ULDTR2_AlignLightOn = ProcedureHelper_Exc.ULDTR2_AlignLightOn ? false : true;
                    }
                    break;
                case "ULDST_PreAlign":
                    {
                        ProcedureHelper_Exc.ULDST_PreAlignLightOn = ProcedureHelper_Exc.ULDST_PreAlignLightOn ? false : true;
                    }
                    break;
                case "ULDST_FinalAlign":
                    {
                        ProcedureHelper_Exc.ULDST_FinalAlignLightOn = ProcedureHelper_Exc.ULDST_FinalAlignLightOn ? false : true;
                    }
                    break;
                case "Efem_Ring":
                    {
                        ProcedureHelper_Exc.Efem_RingLightOn = ProcedureHelper_Exc.Efem_RingLightOn ? false : true;
                    }
                    break;
                case "Efem_Coaxial":
                    {
                        ProcedureHelper_Exc.Efem_CoaxialLightOn = ProcedureHelper_Exc.Efem_CoaxialLightOn ? false : true;
                    }
                    break;
                #endregion                
            }
        }

        private void FreshStatus()
        {
            btnExc_Ball.Monitor(ProcedureHelper_Exc.ExcST_BallLightOn, ProcedureHelper_EQ.ManualMode);
            btnExc_Coaxial.Monitor(Kernel.Exc.DO.Y1F1_CoaxialLight_Exc == 1, ProcedureHelper_EQ.ManualMode);
            btnSortST_PreAlign.Monitor(Kernel.Exc.DO.Y1F2_PreAlignLight_SortST == 1, ProcedureHelper_EQ.ManualMode);
            btnSortST_FinalAlign.Monitor(Kernel.Exc.DO.Y1F3_FinalAlignLight_SortST == 1, ProcedureHelper_EQ.ManualMode);
            btnLaserST_UpperCoaxial.Monitor(Kernel.Exc.DO.Y1F4_UpperCoaxialLight_Laser == 1, ProcedureHelper_EQ.ManualMode);
            btnLaserST_SideCoaxial.Monitor(Kernel.Exc.DO.Y1F5_SideCoaxialLight_Laser == 1, ProcedureHelper_EQ.ManualMode);
            btnLaserST_InspCoaxial.Monitor(Kernel.Exc.DO.Y1F6_InspCoaxialLight_Laser == 1, ProcedureHelper_EQ.ManualMode);
            btnULDTR1_Align.Monitor(Kernel.Exc.DO.Y1F7_AlignLight_ULDTR1 == 1, ProcedureHelper_EQ.ManualMode);
            btnULDTR2_Align.Monitor(Kernel.Exc.DO.Y1F8_AlignLight_ULDTR2 == 1, ProcedureHelper_EQ.ManualMode);
            btnULDST_PreAlign.Monitor(Kernel.Exc.DO.Y1F9_PreAlignLight_ULDST == 1, ProcedureHelper_EQ.ManualMode);
            btnULDST_FinalAlign.Monitor(Kernel.Exc.DO.Y1FA_FinalAlignLight_ULDST == 1, ProcedureHelper_EQ.ManualMode);
            btnEfem_Ring.Monitor(Kernel.Exc.DO.Y1FB_RingLight_Efem == 1, ProcedureHelper_EQ.ManualMode);
            btnEfem_Coaxial.Monitor(Kernel.Exc.DO.Y1FC_CoaxialLight_Efem == 1, ProcedureHelper_EQ.ManualMode);
        }


    }
}
